var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/runoob';
var ObjectId = require('mongodb').ObjectID;
var Q = require('q');
var DB;

MongoClient.connect(DB_CONN_STR, function(err,db) {
    if(err) return console.log(err.message)
    console.log('连接成功')
    DB = db
})

function Manager () {}

//插入数据
Manager.prototype.insert = function (collectionName, data, callback) {
    //连接到表
    let collection = DB.collection(collectionName)
    collection.insert(data, function(err, result) {
        if(err){
            console.log('Error:'+err)
            callback({success:false,result:err,message:'insert'+collectionName+'失败'})
            return 
        }
        callback({success:true,result:result,message:'插入数据成功'});
    })
}

//查找数据
Manager.prototype.find = function (collectionName, query, sort, callback) {
    //连接到表
    let collection = DB.collection(collectionName)
    let _sort = sort || {_id:-1};
    //将查到的数据转换成数组输出
    collection.find(query,{sort: _sort}).toArray(function(err, result) {
        if(err){
            callback({success:false,result:err,message:'find'+collectionName+'失败'})
            return;
        }
        callback({success:true,result:result,message:'查询完成'});
    });
}

//查找一条数据
Manager.prototype.findOne = function (collectionName, whereData, callback){
    //连接到表
    let collection = DB.collection(collectionName)
    collection.findOne(whereData,function(err, result){
        if(result){
            callback({success:true,result:result,message:'查询完成'})
            return 
        }
        callback({success:false,result:err,message:'没有查找到数据'})
    }) 
}

//更新数据
Manager.prototype.update = function (collectionName, whereData, updateDat){
    let deferred = Q.defer();
    let collection = DB.collection(collectionName)
    let id = ObjectId(whereData._id)
    collection.update({_id: id}, updateDat, function(err, result){
        if(err){
            deferred.reject({success:false,result:err,message:'update'+collectionName+'失败'})
        }else{
            deferred.resolve({success:true,result:result,message:'更新完成'})
        }
    })
    return deferred.promise
}

//删除数据
Manager.prototype.delete = function (collectionName, whereData){
    let deferred = Q.defer();
    let collection = DB.collection(collectionName)
    let id = ObjectId(whereData._id)
    collection.remove({_id: id}, function(err, result){
        if(err){
            deferred.reject({success:false,result:err,message:'delete'+collectionName+'失败'})
        }else{
            deferred.resolve({success:true,result:result,message:'删除成功'})
        }
    })
    return deferred.promise
}
//批量删除
Manager.prototype.batchDelete = function (collectionName, whereData){
    let deferred = Q.defer();
    let collection = DB.collection(collectionName)
    let arryId=[]
    whereData.map(currentValue =>{
        let objId = ObjectId(currentValue._id)
        console.log(typeof objId)
        arryId.push(objId)
    })
    console.log(arryId)
    collection.remove({'_id':{$in:arryId}}, function(err, result){
        if(err){
            deferred.reject({success:false,result:err,message:'batchDelete'+collectionName+'失败'})
        }else{
            deferred.resolve({success:true,result:result,message:'批量删除成功'})
        }
    })
    return deferred.promise
}
module.exports = Manager